
#include "m4g6Ctrl.h"
#include "uart.h"
#include "temperature.h"
#include "crc16.h"

u8 SpeakCmd[4]    = {0x01, 0x01, 0x40, 0x00};
u8 yuyin_state[2] = {0};

//语音计时
u16 M4G6_ct = 0;
void M4G6_DataDeal(u8 dat)
{
    switch (dat)
    {
        case 0:  //"接收超时"
            // SpeakCmd[0] = 0;
            // SpeakCmd[1] = 1;
            // SpeakCmd[2] = 0x18;
            // SpeakCmd[3] = 0;
            // write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"没有听清"
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 1:  //"你好小迪"
            SpeakCmd[0] = 1;
            SpeakCmd[1] = 1;
            SpeakCmd[2] = 0x18;
            SpeakCmd[3] = 0;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"主人我在"

            // write_dgus_vp(TEST_VP1, SpeakCmd, 2);
            do
            {
                read_dgus_vp(SPEAK_VP, (u8 *)&SpeakCmd[0], 1);
            } while (SpeakCmd[1]);
            yuyin_state[0] = 1;
            yuyin_state[1] = 0;

            break;
        case 2:  //"升高温度"
            SpeakCmd[0] = 2;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"当前温度"

            yuyin_state[0] = 2;
            yuyin_state[1] = 1;  // 0201,表示2级菜单，温度增加
            DelayMs(1500);
            SpeakCmd[0] = Temperature_Real / 100;
            if (SpeakCmd[0] != 0)
            {
                SpeakCmd[0] += 29;
                SpeakCmd[1] = 1;
                write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"温度十位"
                DelayMs(1500);
            }
            SpeakCmd[0] = (Temperature_Real % 100) / 10 + 20;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"温度个位"

            DelayMs(1000);
            SpeakCmd[0] = 40;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"主人想要多少度"

            DelayMs(2500);
            break;
        case 3:  //"开始制冷"
            SpeakCmd[0] = 5;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"制冷启动"

            // Speed_Auto_Set_Handler();
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
            // //"设定风速"
            // SpeakCmd[0] = 03;
            // SpeakCmd[1] = 1;
            // write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"当前风速"
            // DelayMs(1000);

            // SpeakCmd[0] = Wind_Speed_Val + 20;
            // SpeakCmd[1] = 1;
            // write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"档位"
            // DelayMs(500);
            // SpeakCmd[0] = 4;
            // SpeakCmd[1] = 1;
            // write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"想要多少档"
            // DelayMs(500);
            // break;
        case 4:
            SpeakCmd[0] = 6;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"制热启动"
                                                   //    Mode = 2;
                                                   //            write_dgus_vp(MODE, (u8 *)&Mode, 1);
                                                   //            Speed_Auto_Set_Handler();
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 5:
            SpeakCmd[0] = 7;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"停止工作"

            //            Standby_Handler();  //休眠
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 6:
            SpeakCmd[0] = 8;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"已调节设备"
                                                   //            if (Mode == 1)
                                                   //            {
                                                   //                Speed_Low_Set_Handler();
                                                   //            }
                                                   //            else
                                                   //            {
                                                   //                Wind_Speed++;
                                                   //                if (Wind_Speed > 3)
                                                   //                {
                                                   //                    Wind_Speed = 3;
                                                   //                }
                                                   //                write_dgus_vp(SPEED, (u8 *)&Wind_Speed, 1);
                                                   //            }
                                                   //            Temperature_Set_Increase();
                                                   //            Temperature_Set_Confirm_Handler();
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 7:
            SpeakCmd[0] = 8;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"已调节设备"
                                                   //            if (Mode == 1)
                                                   //            {
                                                   //                Close_Valve_Flag  = 0x01;
                                                   //                Valve_Config_Flag = 0;
                                                   //                TC_Status         = 2;
                                                   //                TC_Status_Old     = TC_Status;
            //                write_dgus_vp(SWITCH, (u8 *)&TC_Status, 1);  //温控器状态休眠
            //            }
            //            else
            //            {
            //                Speed_High_Set_Handler();
            //                Temperature_Set_Increase();
            //                Temperature_Set_Increase();
            //                read_dgus_vp(TEMPERATURE_PRECISION, (u8 *)&ii, 1);
            //                if (ii == 0)
            //                {
            //                    Temperature_Set_Increase();
            //                    Temperature_Set_Increase();
            //                }
            //                Temperature_Set_Confirm_Handler();
            //            }
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 8:
            SpeakCmd[0] = 8;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"已调节设备"
                                                   //            if (Mode == 2)
                                                   //            {
                                                   //                Speed_Low_Set_Handler();
                                                   //            }
                                                   //            else
                                                   //            {
                                                   //                Wind_Speed++;
                                                   //                if (Wind_Speed > 3)
                                                   //                {
                                                   //                    Wind_Speed = 3;
                                                   //                }
                                                   //                write_dgus_vp(SPEED, (u8 *)&Wind_Speed, 1);
                                                   //            }
                                                   //            Tmeperature_Set_Decrease();
                                                   //            Temperature_Set_Confirm_Handler();
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 9:
            SpeakCmd[0] = 8;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"已调节设备"
                                                   //            if (Mode == 2)
                                                   //            {
                                                   //                Close_Valve_Flag  = 0x01;
                                                   //                Valve_Config_Flag = 0;
                                                   //                TC_Status         = 2;
                                                   //                TC_Status_Old     = TC_Status;
            //                write_dgus_vp(SWITCH, (u8 *)&TC_Status, 1);  //温控器状态休眠
            //            }
            //            else
            //            {
            //                Speed_High_Set_Handler();
            //                Tmeperature_Set_Decrease();
            //                Tmeperature_Set_Decrease();
            //                read_dgus_vp(TEMPERATURE_PRECISION, (u8 *)&ii, 1);
            //                if (ii == 0)
            //                {
            //                    Tmeperature_Set_Decrease();
            //                    Tmeperature_Set_Decrease();
            //                }
            //                Temperature_Set_Confirm_Handler();
            //            }
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 10:
            SpeakCmd[0] = 18;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"当前天气"
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 11:
            SpeakCmd[0] = 9;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"开启闹钟"
                                                   //            Alarm_Key[0] = 1;
                                                   //            Alarm_Key[1] = 0;
                                                   //            write_dgus_vp(ALARM_KEY, (u8 *)Alarm_Key, 2);
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 12:
            SpeakCmd[0] = 10;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"关闭闹钟"
                                                   //            Alarm_Key[0] = 0;
                                                   //            Alarm_Key[1] = 0;
                                                   //  write_dgus_vp(ALARM_KEY, (u8 *)Alarm_Key, 2);
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 13:
            // 							SpeakCmd[0] = 11;
            // 							SpeakCmd[1] = 1;
            // 							write_dgus_vp(SPEAK_VP,SpeakCmd,2);		 //"当前状态"

            SpeakCmd[0] = 2;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"当前温度"

            yuyin_state[0] = 0;
            yuyin_state[1] = 0;  // 0202,表示2级菜单，温度降低
            DelayMs(1000);
            SpeakCmd[0] = Temperature_Real / 100;
            if (SpeakCmd[0] != 0)
            {
                SpeakCmd[0] += 29;
                SpeakCmd[1] = 1;
                write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"温度十位"
                DelayMs(1000);
            }
            SpeakCmd[0] = (Temperature_Real % 100) / 10 + 20;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"温度个位"
            DelayMs(1000);

            SpeakCmd[0] = 14;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"当前风速"

            DelayMs(1700);
            // 								read_dgus_vp(SPEED,(u8*)&Wind_Speed,1);
            SpeakCmd[0] = 3 + 20;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);
            // read_dgus_vp(MODE, (u8 *)&Mode, 1);
            // SpeakCmd[0] = Mode + 14;
            // write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"模式"
            // yuyin_state[0] = 0;
            // yuyin_state[1] = 0;
            break;
        case 14:
            SpeakCmd[0] = 8;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"已调节设备"
            // Wind_Speed--;
            // if (Wind_Speed < 1)
            // {
            //     Wind_Speed = 1;
            // }
            // write_dgus_vp(SPEED, (u8 *)&Wind_Speed, 1);
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 15:
            SpeakCmd[0] = 8;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"已调节设备"
            // Wind_Speed++;
            // if (Wind_Speed > 3)
            // {
            //     Wind_Speed = 3;
            // }
            // write_dgus_vp(SPEED, (u8 *)&Wind_Speed, 1);
            yuyin_state[0] = 0;
            yuyin_state[1] = 0;
            break;
        case 16:  //"降低温度"
            SpeakCmd[0] = 2;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"当前温度"

            yuyin_state[0] = 2;
            yuyin_state[1] = 2;  // 0202,表示2级菜单，温度降低
            DelayMs(1500);
            SpeakCmd[0] = Temperature_Real / 100;
            if (SpeakCmd[0] != 0)
            {
                SpeakCmd[0] += 29;
                SpeakCmd[1] = 1;
                write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"温度十位"
                DelayMs(1500);
            }
            SpeakCmd[0] = (Temperature_Real % 100) / 10 + 20;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"温度个位"

            DelayMs(1000);
            SpeakCmd[0] = 40;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"主人想要多少度"

            DelayMs(2500);
            break;
        case 17:  //"调整一度"
        case 18:  //"调整两度"
        case 19:  //"调整三度"
        case 20:  //"调整四度"
        case 21:  //"调整五度"
        case 22:  //"调整六度"
        case 23:  //"调整七度"
        case 24:  //"调整八度"
        case 25:  //"调整九度"

            write_dgus_vp(0x1110, yuyin_state, 1);
            if ((yuyin_state[0] == 2) && (yuyin_state[1] == 1))  //增加温度
            {
                yuyin_state[0] = 0;
                yuyin_state[1] = 0;
                // read_dgus_vp(SET_TEMP_VALUE, (u8 *)&Ttemp, 1);
                // Ttemp = Ttemp + dat - 16;
                // if (Ttemp > 32)
                // {
                //     Ttemp = 32;
                // }
                // write_dgus_vp(SET_TEMP_VALUE, (u8 *)&Ttemp, 1);
                // Ttemp = Ttemp * 10;
                // write_dgus_vp(TEMP_SET, (u8 *)&Ttemp, 1);
                // SpeakCmd[0] = 10;
                // SpeakCmd[1] = 1;
                // write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"已调节设备"
            }
            else if ((yuyin_state[0] == 2) && (yuyin_state[1] == 2))  //降低温度
            {
                yuyin_state[0] = 0;
                yuyin_state[1] = 0;
                // read_dgus_vp(SET_TEMP_VALUE, (u8 *)&Ttemp, 1);
                // Ttemp = Ttemp - dat + 16;
                // if (Ttemp < 16)
                // {
                //     Ttemp = 16;
                // }
                // write_dgus_vp(SET_TEMP_VALUE, (u8 *)&Ttemp, 1);
                // Ttemp = Ttemp * 10;
                // write_dgus_vp(TEMP_SET, (u8 *)&Ttemp, 1);
                // SpeakCmd[0] = 10;
                // SpeakCmd[1] = 1;
                // write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"已调节设备"
            }
            SpeakCmd[0] = 8;
            SpeakCmd[1] = 1;
            write_dgus_vp(SPEAK_VP, SpeakCmd, 2);  //"已调节设备"
            break;
        default:
            //  page_set[0] = 0x00;
            break;
    }

    // write_dgus_vp(PIC_SET, page_set, 2);
    M4G6_ct = 10000;
}

void M4G6_dataRx(void)
{
    u16 rxp;
    u8 temp;
    SpeakCmd[2] = 0x18;
    SpeakCmd[3] = 0;
    // write_dgus_vp(0x5800, (u8 *)&uart3_rx_count, 1);
    if (uart3_rx_count >= 5)
    {
        // write_dgus_vp(TEST_VP4, Uart3_Rx, 3);
        if ((Uart3_Rx[0] == 0x5A) && (Uart3_Rx[1] == 0xA5))
        {
            write_dgus_vp(0x5800, Uart3_Rx, 3);
            rxp = crc16table(Uart3_Rx, 3);
            if (rxp == (Uart3_Rx[3] << 8) | Uart3_Rx[4])
            {
                temp = Uart3_Rx[2];
                memcpy(Uart3_Rx, Uart3_Rx + 5, uart3_rx_count - 5);
                uart3_rx_count -= 5;
                M4G6_DataDeal(temp);
            }
            else
            {
                memcpy(Uart3_Rx, Uart3_Rx + 1, uart3_rx_count - 1);
                uart3_rx_count -= 1;
            }
        }
        else
        {
            memcpy(Uart3_Rx, Uart3_Rx + 1, uart3_rx_count - 1);
            uart3_rx_count -= 1;
        }
    }
}
